package com.heu.blood.storge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heu.blood.bag.entity.BloodBagNewRecycleEntity;
import com.heu.blood.charge.entity.BloodChargeEntity;
import com.heu.blood.charge.vo.BloodChargeEntityVo;
import com.heu.blood.common.Constant.ConstantData;
import com.heu.blood.common.enums.BloodInformationEnum;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.match.dao.BloodInformationDao;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.storge.dao.BloodInStorgeDao;
import com.heu.blood.storge.dictenum.OutStorageReasonEnum;
import com.heu.blood.storge.entity.BloodInStorgeEntity;
import com.heu.blood.storge.entity.BloodOutStorgeEntity;
import com.heu.blood.storge.service.BloodInStorgeService;
import com.heu.blood.storge.service.BloodOutStorgeService;
import com.heu.blood.storge.vo.BloodScrapEntityVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.storge.dao.BloodScrapDao;
import com.heu.blood.storge.entity.BloodScrapEntity;
import com.heu.blood.storge.service.BloodScrapService;
import org.springframework.transaction.annotation.Transactional;


@Service("bloodScrapService")
public class BloodScrapServiceImpl extends ServiceImpl<BloodScrapDao, BloodScrapEntity> implements BloodScrapService {
    @Autowired
    private BloodInformationDao bloodInformationDao;

    @Autowired
    private BloodInStorgeDao bloodInStorgeDao;

    @Autowired
    private BloodInStorgeService bloodInStorgeService;

    @Autowired
    private BloodInformationService bloodInformationService;

    @Autowired
    private BloodOutStorgeService bloodOutStorgeService;

    @Override
    public PageUtils queryPage(BloodScrapEntityVo bloodScrapEntityVo) {
        // 分页信息先搞定
        Map<String,Object> params = new HashMap<>();
        params.put("limit", bloodScrapEntityVo.getLimit());
        params.put("page", bloodScrapEntityVo.getPage());
        // 下面处理筛选的信息
            LambdaQueryWrapper<BloodScrapEntity> queryWrapper = Wrappers.lambdaQuery(BloodScrapEntity.class);
        queryWrapper.like(Strings.isNotBlank(bloodScrapEntityVo.getBloodId()), BloodScrapEntity::getBloodId, bloodScrapEntityVo.getBloodId())
                .like(Strings.isNotBlank(bloodScrapEntityVo.getOperatorId()), BloodScrapEntity::getOperatorId, bloodScrapEntityVo.getOperatorId())
                .like(Strings.isNotBlank(bloodScrapEntityVo.getOperatorName()), BloodScrapEntity::getOperatorName, bloodScrapEntityVo.getOperatorName())
                .like(Strings.isNotBlank(bloodScrapEntityVo.getBloodScrapReason()), BloodScrapEntity::getBloodScrapReason, bloodScrapEntityVo.getBloodScrapReason());
        queryWrapper.orderByDesc(BloodScrapEntity::getGmtCreate);
        IPage<BloodScrapEntity> page = this.page(
                new Query<BloodScrapEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional // 加上事务，因为要更新两个状态，一个是入库表状态，一个血液信息表状态
    public boolean scrap(BloodScrapEntityVo bloodScrapEntityVo) {
//        分两步1.把BloodInformation和BloodInstorgeEntity的state变成scrap2.在报废表插入该条bloodInformation
        LambdaQueryWrapper<BloodInformationEntity> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<BloodInStorgeEntity> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodScrapEntityVo.getBloodId()), BloodInformationEntity::getBloodId, bloodScrapEntityVo.getBloodId());
        queryWrapper2.eq(Strings.isNotBlank(bloodScrapEntityVo.getBloodId()), BloodInStorgeEntity::getBloodId, bloodScrapEntityVo.getBloodId());
//        查到所有BloodId为给定值的List
        List<BloodInformationEntity> list = new ArrayList<>();
        List<BloodInStorgeEntity> list2 = new ArrayList<>();
        if (Strings.isNotBlank(bloodScrapEntityVo.getBloodId())) {
            list = bloodInformationDao.selectList(queryWrapper);
            list2 = bloodInStorgeDao.selectList(queryWrapper2);
        }
//        更改BloodInStorgeEntity的状态
        for (BloodInStorgeEntity entity: list2) {
//            已经报废的不要在重新报废了
            if (entity.getBloodState().equals(BloodInformationEnum.BLOOD_SCRAP.code())) {
                continue;
            } else {
                entity.setBloodState(BloodInformationEnum.BLOOD_SCRAP.code());
            }
        }
        if (!list2.isEmpty()) {
            bloodInStorgeService.updateBatchById(list2);
        }
        List<BloodScrapEntity> result = new ArrayList<>();
        List<BloodOutStorgeEntity> outStorgeResult = new ArrayList<>();
        for (BloodInformationEntity entity: list) {
//            已经报废的不要在重新报废了
            if (entity.getBloodState().equals(BloodInformationEnum.BLOOD_SCRAP.code())) {
                continue;
            } else {
                BloodScrapEntity bloodScrapEntity = new BloodScrapEntity();
//            更改state
                entity.setBloodState(BloodInformationEnum.BLOOD_SCRAP.code());
//                bloodInformationDao.updateById(entity);
//            拷贝基本信息
                BeanUtils.copyProperties(entity, bloodScrapEntity);
                bloodScrapEntity.setBloodScrapReason(bloodScrapEntityVo.getBloodScrapReason());
                bloodScrapEntity.setOperatorId(bloodScrapEntityVo.getOperatorId());
                bloodScrapEntity.setOperatorName(bloodScrapEntityVo.getOperatorName());
                bloodScrapEntity.setGmtCreate(bloodScrapEntityVo.getGmtCreate());
                bloodScrapEntity.setGmtModified(bloodScrapEntityVo.getGmtModified());
                result.add(bloodScrapEntity);

                BloodOutStorgeEntity bloodOutStorgeEntity=new BloodOutStorgeEntity();
                BeanUtils.copyProperties(entity, bloodOutStorgeEntity);
                bloodOutStorgeEntity.setOutStorgeReason(OutStorageReasonEnum.SCRAP.code());
                bloodOutStorgeEntity.setOperatorName(bloodScrapEntityVo.getOperatorName());
                bloodOutStorgeEntity.setOperatorId(bloodScrapEntityVo.getOperatorId());
                bloodOutStorgeEntity.setGmtCreate(new Date());
                bloodOutStorgeEntity.setGmtModified(new Date());
                bloodOutStorgeEntity.setOutStorgeTime(new Date());
//            创建一个出库的单据号
                GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(ConstantData.BLOOD_OUT_STORAGE_NO_WORK_ID, ConstantData.BLOOD_OUT_STORAGE_NO_PREFIX);
                String outStorageId = generateNumberBySnowFlake.genNumberByflag();
                bloodOutStorgeEntity.setBloodBankDeliveryId(outStorageId);
                outStorgeResult.add(bloodOutStorgeEntity);
                 }
        }
        if (!list.isEmpty()) {
            bloodInformationService.updateBatchById(list);
        }
        boolean b = this.saveBatch(result);
        //插入到出库表
        boolean outStorgeSaved=bloodOutStorgeService.saveBatch(outStorgeResult);

        return b&&outStorgeSaved;
    }
}